﻿/*------------------------------------------------------------------
-- COPYRIGHT (C) 2010-2012  Atom
-- ALL RIGHTS RESERVED.
-- 兆尹科技
-- CREATE DATE: 2010/07/12
-- CREATE MAN:liujian	
-- 连接字符串加密解密类
-- MODIFY HISTORY:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
---------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;

namespace Atom.Common
{
    /// <summary>
    /// 有key和Value的集合
    /// </summary>
    /// <typeparam name="TKey">Key的类型</typeparam>
    /// <typeparam name="TValue">Value的类型</typeparam>
    public class ListKeyValue<TKey, TValue> : List<KeyValuePair<TKey, TValue>>
    {
        /// <summary>
        /// 添加一笔
        /// </summary>
        /// <param name="ele"></param>
        public new void Add(KeyValuePair<TKey, TValue> ele)
        {
            this.Add(ele.Key, ele.Value);
        }

        /// <summary>
        /// 添加一笔
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public virtual void Add(TKey key, TValue val)
        {
            if(Keys.Contains(key))
            {
                throw new NotImplementedException("已存在相同的Key");
            }
            base.Add(new KeyValuePair<TKey, TValue>(key, val));
        }

        /// <summary>
        /// 根据Key获取Value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual TValue GetValue(TKey key)
        {
            TValue val = default(TValue);

            foreach (var item in this)
            {
                if (typeof(TKey) == typeof(string))
                {
                    if (item.Key.ToString() == key.ToString())
                    {
                        val = item.Value;
                        break;
                    }
                }
                else if (typeof(TKey).IsValueType)
                {
                    if (item.Key.ToString() == key.ToString())
                    {
                        val = item.Value;
                        break;
                    }
                }
                else if (object.ReferenceEquals(item.Key, key))
                {
                    val = item.Value;
                    break;
                }
            }
            
            return val;
        }

        /// <summary>
        /// Key集合
        /// </summary>
        public virtual List<TKey> Keys
        {
            get
            {
                if(this.Count == 0)
                {
                    return new List<TKey>();
                }
                return this.ConvertAll<TKey>(_ele => { return _ele.Key; });
            }
        }

        /// <summary>
        /// Value集合 
        /// </summary>
        public virtual List<TValue> Values
        {
            get
            {
                if (this.Count == 0)
                {
                    return new List<TValue>();
                }
                return this.ConvertAll<TValue>(_ele => { return _ele.Value; });
            }
        }
    }
}
